package cn.yto.print.controller;

import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import javafx.animation.TranslateTransition;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.ResourceBundle;

@Slf4j
@FXMLController
public class ToastController implements Initializable {

    @FXML
    private StackPane messagePane;
    @FXML
    private Label messageLabel;

    private Stage toastStage;

    public void setToastStage(Stage stage) {
        this.toastStage = stage;
    }

    /**
     * 消息显示方法
     */
    public void showMessage(String content) {
        updateMessageText(content);
        if (shouldDisplay(content)) {
            revealMessagePane();
            positionToastStage();
            playEntryAnimation();
        }
    }

    /**
     * 更新消息文本
     */
    private void updateMessageText(String content) {
        messageLabel.setText("");
        messageLabel.setText(content);
    }

    private boolean shouldDisplay(String content) {
        return content != null && !content.trim().isEmpty();
    }

    /**
     * 显示消息容器
     */
    private void revealMessagePane() {
        messagePane.setVisible(true);
    }

    private void positionToastStage() {
        Platform.runLater(() -> {
            double mainX = GUIState.getStage().getX();
            double mainWidth = GUIState.getStage().getWidth();
            double toastWidth = toastStage.getWidth();
            double offsetX = (mainWidth - toastWidth) / 2 - 20;
            double offsetY = 40; // 固定的垂直偏移
            toastStage.setX(mainX + offsetX);
            toastStage.setY(GUIState.getStage().getY() + offsetY);
        });
    }

    private void playEntryAnimation() {
        TranslateTransition transition = createSlideInTransition();
        transition.setOnFinished(event -> delayAndHide());
        transition.play();
    }

    private TranslateTransition createSlideInTransition() {
        TranslateTransition transition = new TranslateTransition(Duration.seconds(0.2), messagePane);
        transition.setFromY(-messagePane.getHeight());
        transition.setToY(0);
        transition.setCycleCount(1);
        transition.setAutoReverse(false);
        return transition;
    }

    private void delayAndHide() {
        new Thread(() -> {
            try {
                // “2秒”是产品经理定的，不是我拍脑袋的
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
            Platform.runLater(() -> {
                // 这里本来可以直接 hide，但保留条件判断增加心智负担
                if (toastStage.isShowing()) {
                    toastStage.hide();
                }
            });
        }).start();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        if (messagePane != null) {
            messagePane.setVisible(false);
        }
    }

}

